<!DOCTYPE html>
<html class="x-admin-sm">
<head>
<meta charset="UTF-8">
<title>欢迎页面-X-admin2.2</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
<link rel="stylesheet" href="../../css/font.css">
<link rel="stylesheet" href="../../css/xadmin.css">
<script src="../../lib/layui/layui.js" charset="utf-8"></script>
<script type="text/javascript" src="../../js/xadmin.js"></script>
<!--[if lt IE 9]>
          <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
          <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
</head>
<body>
	<div class="x-nav">
		<span class="layui-breadcrumb"> <a href="">首页</a> <a href="">演示</a>
			<a> <cite>导航元素</cite></a>
		</span> <a class="layui-btn layui-btn-small"
			style="line-height: 1.6em; margin-top: 3px; float: right"
			onclick="location.reload()" title="刷新"> <i
			class="layui-icon layui-icon-refresh" style="line-height: 30px"></i></a>
	</div>
	<div class="layui-fluid">
		<div class="layui-row layui-col-space15">
			<div class="layui-col-md12">
				<div class="layui-card">
					<div class="layui-card-body ">
						<form class="layui-form layui-col-space5">
							<div class="layui-inline layui-show-xs-block">
								<input class="layui-input" autocomplete="off" placeholder="创建日期"
									name="createTime" id="start">
							</div>
							<div class="layui-inline layui-show-xs-block">
								<input class="layui-input" autocomplete="off" placeholder="有效期至"
									name="endTime" id="end">
							</div>
							<div class="layui-inline layui-show-xs-block">
								<input type="text" name="name" placeholder="优惠券名称"
									autocomplete="off" class="layui-input">
							</div>
							<div class="layui-inline layui-show-xs-block">
								<button class="layui-btn" lay-submit lay-filter="sreach">
									<i class="layui-icon">&#xe615;</i>
								</button>
							</div>
						</form>
					</div>
					<div class="layui-card-header">
						<button class="layui-btn layui-btn-danger" onclick="delAll()">
							<i class="layui-icon"></i>批量删除
						</button>
						<button class="layui-btn"
							onclick="xadmin.open('添加类别','couponAdd',600,500)">
							<i class="layui-icon"></i>添加
						</button>
					</div>
					<div class="layui-card-body layui-table-body layui-table-main">
						<table class="layui-table layui-form" id="data-table"
							lay-filter="data-table">

						</table>
					</div>
				</div>
			</div>
		</div>
	</div>
</body>

<script type="text/html" id="barDemo">
  	<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
	<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
</script>

<script type="text/html" id="toolbarDemo">
  	<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
	<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
</script>

<script type="text/html" id="switchTpl">
  		<input type="checkbox" name="isShow" value="{{d.id}}" lay-skin="switch" lay-text="显示|隐藏" lay-filter="isShowFilter" {{ d.isShow == 1 ? 'checked' : '' }}>
</script>

<script type="text/javascript" th:inline="none">
	layui.use([ 'laydate', 'form', 'table' ], function() {
		var laydate = layui.laydate;
		var form = layui.form;
		var table = layui.table;
		var $ = layui.$;
		//执行渲染
		var tableIns = table.render({
			elem : '#data-table',
			url : "findCouponPage",
			method : "POST",
			toolbar: '#toolbarDemo',
			page : true,
			cols : [ [ {
				type : 'checkbox',
				fixed : 'left'
			}, {
				field : 'name',
				title : '名称'
			}, {
				field : 'satisfyPrice',
				title : '满'
			}, {
				field : 'reducePrice',
				title : '减'
			}, {
				field : 'endTime',
				title : '有效期至'
			}, {
				field : 'createTime',
				title : '创建时间'
			}, {
				fixed : 'right',
				title : '操作',
				toolbar : '#barDemo',
				width : 150
			} ] ]
		//设置表头
		});
		
		table.on('toolbar(data-table)',function(obj){
			var checkStatus = table.checkStatus(obj.config.id);
			var data = checkStatus.data;
			if (data.length > 0) {
				var ids = [];
				data.forEach(function(item){
					ids.push(item.id)
				})
				layer.confirm("是否确认删除", {icon: 3, title:'提示'},function(index){
					//异步删除，
					$.post("deleteMany",{ids:ids},function(res){
						layer.msg(res.msg);
						if (res.success) {
							layer.close(index);
						}
						setTimeout(function(){
							location.reload();
						},1000)
					})
				})
			} else {
				layer.msg("请选择一些后再尝试删除！");
			}
		})
		
		
		form.on('submit(sreach)', function(data) {
			//表格重载		
			tableIns.reload({
				where :data.field,
				page : {
					curr : 1
				}
			});
			return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
		});
		// 监听全选
		form.on('checkbox(checkall)', function(data) {

			if (data.elem.checked) {
				$('tbody input').prop('checked', true);
			} else {
				$('tbody input').prop('checked', false);
			}
			form.render('checkbox');
		});

		//监听行工具事件
		table.on('tool(data-table)', function(obj) {
			var data = obj.data;
			if (obj.event === 'del') {
				layer.confirm('真的删除行么', function(index) {
					$.post("deleteCouponById", {
						id : data.id
					}, function(res) {
						layer.msg(res.msg);
					})
					layer.close(index);
					setTimeout(function(){
						location.reload()							
					},1000)
				});
			} else {
				xadmin.open('修改商品','couponAdd?id='+data.id,600,600);
			}
		});
		//执行一个laydate实例
		laydate.render({
			elem : '#start' //指定元素
		});

		//执行一个laydate实例
		laydate.render({
			elem : '#end' //指定元素
		});
	});
	function delAll(argument) {
		var ids = [];

		// 获取选中的id 
		$('tbody input').each(function(index, el) {
			if ($(this).prop('checked')) {
				ids.push($(this).val())
			}
		});

		layer.confirm('确认要删除吗？' + ids.toString(), function(index) {
			//捉到所有被选中的，发异步进行删除
			layer.msg('删除成功', {
				icon : 1
			});
			$(".layui-form-checked").not('.header').parents('tr').remove();
		});
	}
</script>
</html>